package com.msb.servicemap.service;

import com.msb.internalcommon.constant.AmapConfigConstants;
import com.msb.internalcommon.constant.CommonStatusEnum;
import com.msb.internalcommon.dto.DicDistrict;
import com.msb.internalcommon.dto.ResponseResult;
import com.msb.servicemap.mapper.DicDistrictMapper;
import com.msb.servicemap.remote.MapDicDistrictClient;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * @Auther: jiudianliu
 * @Date: 2023/6/9 - 06 - 09 - 15:00
 * @Description: com.msb.servicemap.service
 * @version: 1.0
 */
@Service
public class DicDistrictService {

    @Autowired
    private MapDicDistrictClient mapDicDistrictClient ;

    @Autowired
    private DicDistrictMapper dicDistrictMapper ;

    public ResponseResult initDicDistrict(String keywords){

        //请求地图
        String dicDistrictResult = mapDicDistrictClient.DicDistrict(keywords);
        System.out.println(dicDistrictResult);

        //解析结果
        JSONObject dicDistrictJsonObject = JSONObject.fromObject(dicDistrictResult);
        int status = dicDistrictJsonObject.getInt(AmapConfigConstants.STATUS);
        if (status != 1){
            return ResponseResult.fail(CommonStatusEnum.MAP_DISTRICT_ERROR.getCode() , CommonStatusEnum.MAP_DISTRICT_ERROR.getValue());
        }
        JSONArray countryJsonArray = dicDistrictJsonObject.getJSONArray(AmapConfigConstants.DISTRICTS);
        for (int country = 0 ; country<countryJsonArray.size() ; country++){
            JSONObject countryJsonObject = countryJsonArray.getJSONObject(country);
            String countryAddressCode = countryJsonObject.getString(AmapConfigConstants.ADCODE);
            String countryAddressName = countryJsonObject.getString(AmapConfigConstants.NAME);
            String countryLevel = countryJsonObject.getString(AmapConfigConstants.LEVEL);
            String countryParentAddressCode = "0";
            //插入数据库
            insertDicDistrict(countryAddressCode , countryAddressName , countryLevel , countryParentAddressCode);

            JSONArray proviceJsonArray = countryJsonObject.getJSONArray(AmapConfigConstants.DISTRICTS);
            for (int p = 0 ; p<proviceJsonArray.size() ; p++) {
                JSONObject provicJsonObject = proviceJsonArray.getJSONObject(p);
                String provicAddressCode = provicJsonObject.getString(AmapConfigConstants.ADCODE);
                String provicAddressName = provicJsonObject.getString(AmapConfigConstants.NAME);
                String provicLevel = provicJsonObject.getString(AmapConfigConstants.LEVEL);
                String provicParentAddressCode = countryAddressCode;
                //插入数据库
                insertDicDistrict(provicAddressCode, provicAddressName, provicLevel, provicParentAddressCode);

                JSONArray cityJsonArray = provicJsonObject.getJSONArray(AmapConfigConstants.DISTRICTS);
                for (int c = 0 ; c<cityJsonArray.size() ; c++) {
                    JSONObject cityJsonObject = cityJsonArray.getJSONObject(c);
                    String cityAddressCode = cityJsonObject.getString(AmapConfigConstants.ADCODE);
                    String cityAddressName = cityJsonObject.getString(AmapConfigConstants.NAME);
                    String cityLevel = cityJsonObject.getString(AmapConfigConstants.LEVEL);
                    String cityParentAddressCode = provicAddressCode;
                    //插入数据库
                    insertDicDistrict(cityAddressCode, cityAddressName, cityLevel, cityParentAddressCode);

                    JSONArray districtArrayJsonArray = cityJsonObject.getJSONArray(AmapConfigConstants.DISTRICTS);
                    for (int d = 0 ;d<districtArrayJsonArray.size() ; d++) {
                        JSONObject districtJsonObject = districtArrayJsonArray.getJSONObject(d);
                        String districtAddressCode = districtJsonObject.getString(AmapConfigConstants.ADCODE);
                        String districtAddressName = districtJsonObject.getString(AmapConfigConstants.NAME);
                        String districtLevel = districtJsonObject.getString(AmapConfigConstants.LEVEL);
                        String districtParentAddressCode = cityAddressCode;
                        if(districtLevel.equals(AmapConfigConstants.STREET)){
                            continue;
                        }

                        //插入数据库
                        insertDicDistrict(districtAddressCode, districtAddressName, districtLevel, districtParentAddressCode);
                    }
                }
            }
        }
        return ResponseResult.success();
    }

    public void insertDicDistrict(String addressCode , String addressName , String level , String parentAddressCode ){
        //数据库对象
        DicDistrict district = new DicDistrict();
        district.setAddressCode(addressCode);
        district.setAddressName(addressName);
        int levelInt = generateLevel(level);
        district.setLevel(levelInt);

        //插入数据库
        district.setParentAddressCode(parentAddressCode);
        dicDistrictMapper.insert(district);
    }


    public int generateLevel(String level){
        int levelInt = 0 ;
        if (level.trim().equals("county")){
            levelInt = 0;
        }else if (level.trim().equals("province")){
            levelInt = 1 ;
        }else if (level.trim().equals("city")){
            levelInt = 2 ;
        }else if (level.trim().equals("district")){
            levelInt = 3 ;
        }
        return levelInt;
    }
}
